package oauth2.interceptor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Value("${OAuth2.getCodeURI}")
    private String getCodeURI;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        HttpSession session = request.getSession();
        //是否登录
        if (session.getAttribute("user") != null ) {
            return true;
        }else {
            response.sendRedirect(getCodeURI + request.getRequestURI());
            return false;
        }

    }
}
